var tableElem = "LAY-home-casesCategory-list";
var formElem = "LAY-home-casesCategory-listform";
var tableToolsElem = "LAY-home-casesCategory-tools";
var formSubmitElem = "LAY-home-casesCategory-listform-submit";
var formUrl = '/home/casesCategory/listform'
var historyUrl = '/home/casesCategory/list'
var baseUrl = window.globalConfig.url_caseCategory

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , table = layui.table
        , form = layui.form

    ;

    //列表管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , lineStyle: 'height: 80px;'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id',  title: 'id', fixed: 'left', sort: true}
            , {field: 'name', title: '分类名称', minWidth: 250, edit: 'text'}
            , {field: 'is_show', title: '是否显示', templet: "#ID-table-casesCategory-is_show-switch"}
            , {field: 'sort', title: '排序', edit: 'text'}
             , {
                field: 'casesCount', title: "案例数量", templet: function (d) {
                    return d.cases.length
                }
            }
            , {title: '操作', minWidth: 380, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'},
        done(d) {
        }
    });

    var active = {
        //添加
        add: function (othis) {
            location.hash = formUrl
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除列表
            layer.confirm('确定删除此列表？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改列表
            location.hash = formUrl + "/id=" + data.id
        }
    });

    /**
     * 表单回调
     * @param d
     */
    layui.data.done = function (d) {
        var remoteData = d.data;
        form.render();
        // 加载上传插件
        common.uploader('ID-upload-casesCategory-btn', 'ID-upload-casesCategory-img', 'photo')

        // 添加分类
        form.on(`submit(${formSubmitElem})`, (data) => {
            var field = data.field; //获取提交的字段
            field.is_show = field.is_show === undefined ? 0 : field.is_show;
            let url = baseUrl;
            let type = "POST";
            if (remoteData && remoteData.id) {
                url += "/" + remoteData.id;
                type = "PUT"
            }

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            admin.req({
                url: url,
                type: type,
                data: field,
                success: function (res) {
                    common.ctmMsg(res.code, res.msg)
                    history.back()
                }
            })
        })
    }

    // 表格文本修改
    common.tableTextEditor(tableElem, baseUrl, 'id')
});